package com.aijk.commons.idworker;


import com.aijk.perf.Perf;

import java.util.HashSet;
import java.util.Set;

/**
 * <per>
 * IdGenerator QPS 性能测试
 * 1、测试并发下的生成QPS
 * 2、测试并发下是否会重复
 * </per>
 */
public class IdGeneratorPerf {

    private static final Set<Long> set = new HashSet<>();

    public static void main(String[] args) throws Exception {

        // 为了排除初始化节点的耗时干扰，调用一次完成zk初始化
        IdGenerator.INSTANCE.nextId();

        Perf perf = new Perf() {

            @Override
            public TaskInThread buildTaskInThread() {
                return new TaskInThread() {

                    @Override
                    public void initTask() throws Exception {

                    }

                    @Override
                    public void doTask() throws Exception {
                        long id = IdGenerator.INSTANCE.nextId();
                        if (set.contains(id)) {
                            throw new RuntimeException("重复的ID");
                        } else {
                            set.add(id);
                        }
                    }
                };
            }

        };
        perf.loopCount = 100000;
        perf.threadCount = 2;
        perf.logInterval = 1000;
        perf.run();
        perf.close();

    }
}
